package com.bb.blog.activiti.controller.action;

import com.bb.blog.activiti.domain.ValidGroup;
import com.bb.blog.activiti.domain.dto.ProcessInsObj;
import com.bb.blog.activiti.domain.param.StartProcessParam;
import com.bb.blog.activiti.service.TaskActionService;
import com.bb.blog.web.component.ResponseUtil;
import lombok.RequiredArgsConstructor;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/action")
@RequiredArgsConstructor
public class ProcessActionController {
    private final RuntimeService runtimeService;
    private final TaskService taskService;
    private final TaskActionService taskActionService;
    /**
     * 启动流程
     *
     * @return 流程信息
     */
    @PostMapping("/startProcessById")
    public Object startProcessById(
            @RequestBody @Validated(value = ValidGroup.StartProcessById.class)
                    StartProcessParam startProcessParam) {
        ProcessInstance instance = runtimeService.startProcessInstanceById(startProcessParam.getProcessId());
        ProcessInsObj po = new ProcessInsObj();
        BeanUtils.copyProperties(instance, po);
        return ResponseUtil.success(po);
    }

    /**
     * 完成任务
     */
    @PostMapping("/complete")
    public Object completeUserTask(@RequestParam String taskId){
        taskService.complete(taskId);
        return ResponseUtil.success("操作成功");
    }

    /**
     * 回滚任务
     */
    @PostMapping("/rollback")
    public Object rollbackTask(@RequestParam String taskId){
        taskActionService.rollbackToPrevious(taskId);
        return ResponseUtil.success("操作成功");
    }

    /**
     * 跳转到指定节点
     */
    @PostMapping("/jump")
    public Object jump(@RequestParam String taskId, @RequestParam String nodeDefKey){
        taskActionService.jump(taskId, nodeDefKey);
        return ResponseUtil.success("操作成功");
    }


}
